package top.drinyeit.weilong.common;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;


/**
 * Created by YJY on 2017/9/15.
 */

public class CommonAdapter<T> extends RecyclerView.Adapter<CommonAdapter<T>.ViewHolder> {

    private ItemClick itemClick;
    private Context context;
    private List<T> data;
    private int layoutId;
    private int variableId;
    private LayoutInflater inflater;

    public CommonAdapter() {
    }

    public CommonAdapter(Context context, List<T> data, @LayoutRes int layoutId, int variableId, @Nullable ItemClick itemClick) {
        this.context = context;
        this.layoutId = layoutId;
        this.data = data;
        this.variableId = variableId;
        this.itemClick = itemClick;
        inflater = LayoutInflater.from(context);
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public void addData(List<T> data) {
        this.data.addAll(data);
    }

    public void clear() {
        this.data.clear();
    }

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, layoutId, parent, false);
        ViewHolder viewHolder = new ViewHolder(binding.getRoot());
        viewHolder.setBinding(binding);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.binding.setVariable(variableId, data.get(position));
        // When a variable or observable changes, the binding will be scheduled to change before the next frame.
        // There are times, however, when binding must be executed immediately.
        // To force execution, use the executePendingBindings() method.
        holder.binding.executePendingBindings();
    }


    @Override
    public int getItemCount() {
        return null == data ? 0 : data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ViewDataBinding binding;

        ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
        }

        void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        @Override
        public void onClick(View v) {
            if (itemClick != null) {
                itemClick.onItemClick(v, getAdapterPosition());
            }
        }
    }
}
